<?php

// Gestion des utilisateurs (existence, vérification du mot de passe, du niveau, etc.)

// Variables de session
$GLOBALS['niveau'] = -1 ;
$GLOBALS['pseudo'] = "" ;

function isUser($user){
    if($user == ""){
        return FALSE;
    }
    connect();
    $requete = "SELECT * FROM User WHERE `pseudo` = \"{$user}\";";
    $reponse = executerRequete($requete, $GLOBALS['debug']);
    $return = (mysql_num_rows($reponse) > 0);
    mysql_close();
    return $return ;
}

// Fonction recherchant un utilisateur
function getUser($user)
{
    connect();
    $requete = "SELECT * FROM User WHERE `pseudo` = \"{$user}\";";
    $reponse = executerRequete($requete, $GLOBALS['debug']);
    $return = mysql_fetch_array($reponse);
    mysql_close();
    return $return ;
}

function isPasswordCorrect($user, $password){
    connect();
    $requete = "SELECT * FROM User WHERE `pseudo` = \"{$user}\";";
    $reponse = executerRequete($requete, $GLOBALS['debug']);
    $resultat = mysql_fetch_array($reponse);
    $return = ($resultat['password'] == md5($password));
    mysql_close();
    return $return ;
}

function userNiveau($user){
    connect();
    $requete = "SELECT * FROM User WHERE `pseudo` = \"{$user}\";";
    $reponse = executerRequete($requete, $GLOBALS['debug']);
    if(mysql_num_rows($reponse) > 0){
        $resultat = mysql_fetch_assoc($reponse);
        $niveau = $resultat['niveau'];
    }else{
        $niveau = 0;
    }
    mysql_close();
    return $niveau ;
}

function pseudo()
{
    if (isset($GLOBALS['pseudo'])) return $GLOBALS['pseudo'] ;
    else return "" ;
}

function niveau(){
    if (isset($GLOBALS["niveau"])) return $GLOBALS["niveau"] ;
    else return -1 ;
}
function niveauOK ($n)
{
    if (niveau() >= $n) return true ;
    else return false ;
}
function initialiser_session()
{
    connect() ;
    $requete = "SELECT * FROM Session WHERE id=\"".session_id()."\";" ;
    $result = executerRequete($requete, $GLOBALS['debug']) ;
    if (mysql_num_rows($result))
    {
        $row = mysql_fetch_assoc($result) ;
        $requete = "SELECT * FROM User WHERE pseudo=\"{$row['pseudo']}\";" ;
        $result = executerRequete($requete, $GLOBALS['debug']) ;
        if (mysql_num_rows($result))
        {
            $row = mysql_fetch_assoc($result) ;
            $GLOBALS['pseudo'] = $row['pseudo'] ;
            $GLOBALS['niveau'] = $row['niveau'] ;
        }
    }
    mysql_close() ;
}
function connecte()
{
    return (!empty($GLOBALS['pseudo'])) ;
}

?>
